﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ALinq.Access;
using ALinq.Mapping;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NorthwindDemo;

namespace TestProject
{
    [TestClass]
    public class AssociationMappingTest
    {
        private const string CategoryTableName = "Categories";
        private const string ProdcutTableName = "Products";

        class NorthwindMappingSource : FluentMappingSource
        {
            public NorthwindMappingSource(Action<DatabaseMapping<NorthwindDatabase>> func = null)
            {
                Map<NorthwindDatabase>(mapping =>
                {
                    mapping.ProviderType = typeof(AccessDbProvider);
                    mapping.Table(o => o.Categories, CategoryTableName)
                           .PrimaryKey(o => o.CategoryID)
                           .Column(o => o.CategoryName)
                           .Column(o => o.Description)
                           .Column(o => o.Picture)
                           .Association(o => o.Products, o => new { o.CategoryID }, o => new { o.CategoryID });

                    mapping.Table(o => o.Products, ProdcutTableName)
                           .PrimaryKey(o => o.ProductID)
                           .Column(o => o.ProductName)
                           .Column(o => o.CategoryID)
                           .Association(o => o.Category, o => o.CategoryID, o => o.CategoryID);

                    if (func != null)
                        func(mapping);
                });
            }
        }

        [TestMethod]
        public void CreateAssociations()
        {
            var mappingSource = new NorthwindMappingSource();
            MetaModel model = mappingSource.GetModel(typeof(NorthwindDatabase));
            var categoryTable = model.GetTable(typeof(Category));
            var productTable = model.GetTable(typeof(Product));

            var associations = categoryTable.RowType.Associations;
            Assert.AreEqual(1, associations.Count);

            associations = productTable.RowType.Associations;
            Assert.AreEqual(1, associations.Count);
        }
    }
}
